﻿using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunlit.House.Helper
{
    public class PinyinHelper
    {
        /// <summary> 
        /// 汉字转化为拼音首字母
        /// </summary> 
        /// <param name="str">汉字</param> 
        /// <returns>首字母</returns> 
        public static string GetFirstPinyin(string str)
        {
            string r = string.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }
        public static string GetFirstLetter(string str)
        {
            try
            {
                if (str.StartsWith("广"))
                {
                    return "G";
                }

                ChineseChar chineseChar = new ChineseChar(str[0]);
                return chineseChar.Pinyins[0].ToString().Substring(0, 1).ToUpper();
            }
            catch (Exception)
            {

                return str.Substring(0, 1);
            }
        }

    }
}
